package net.tv90.business.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import net.tv90.business.model.domain.Subject;
import net.tv90.business.model.dto.SubjectQueryDTO;
import net.tv90.business.model.vo.StudentVO;
import net.tv90.business.model.vo.SubjectVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface SubjectMapper extends BaseMapper<Subject> {

    /**
     * 查询教师提交的课题数量
     */
    default Integer selectSubjectCountSubmittedByTeacher(Long teacherId) {
        QueryWrapper<Subject> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacherId);
        return Math.toIntExact(selectCount(wrapper));
    }

    /**
     * 复杂条件查询
     */
    List<SubjectVO> selectByCondition(SubjectQueryDTO dto);

    /**
     * 根据课题id和教师id删除课题
     */
    @Delete("delete from auth.subject where id = #{id} and teacher_id = #{teacherId} and selected = 0 and student_id IS NULL")
    Integer deleteSubject(Long id, Long teacherId);

    /**
     * 选题
     */
    void selectSubject(Long id, Long studentId, Long teacherId);

    /**
     * 根据课题id查询教师id
     */
    @Select("select teacher_id from auth.subject where id = #{id}")
    Long selectTeacherIdBySubjectId(Long id);

    /**
     * 查询学生预选题的数量
     */
    @Select("select count(*) from auth.selection_record where student_id = #{studentId}")
    Integer selectSubjectCountSelectedByStudent(Long studentId);

    /**
     * 根据课题id查询所有预选的学生名字
     */
    List<StudentVO> selectPreSelectionStudentVO(Long id);

    /**
     * 学生根据课题id和学生id撤回选题
     */
    @Delete("delete from auth.selection_record where subject_id = #{id} and student_id = #{studentId}")
    Integer withdrawSubject(Long id, Long studentId);

    /**
     * 根据学生id查询所有选择的课题id，用于快速查询“我的”选题
     */
    @Select("select subject_id from auth.selection_record where student_id = #{studentId}")
    List<Long> querySubjectIdRecordByStudentId(Long studentId);

    /**
     * 根据学生id和课题id查询数量，防止重复选题
     */
    @Select("select count(*) from auth.selection_record where subject_id = #{id} and student_id = #{studentId}")
    Integer selectRecordCount(Long id, Long studentId);

    /**
     * 根据要新增的课题id，学生id，教师id，查询选题记录中是否存在学生已经选择了该名教师，防止学生重复选择多名教师
     */
//    @Select("SELECT COUNT(*) FROM auth.selection_record WHERE student_id = #{studentId} AND teacher_id = #{teacherId} " +
//            "GROUP BY student_id, teacher_id HAVING COUNT(*) >= 1")
    @Select("select COUNT(*) from auth.selection_record where student_id = #{studentId} and teacher_id = #{teacherId}")
    Long checkDuplicateTeacher(Long id, Long studentId, Long teacherId);

    /**
     * 在选题记录中也删除课题，保证数据库的一致性
     */
    @Delete("delete from auth.selection_record where subject_id = #{id}")
    void deleteSubjectRecord(Long id);

    /**
     * 教师确认学生，第一步，查询该学生在不在选题记录里面
     */
    @Select("select count(*) from auth.selection_record where subject_id = #{id} and student_id = #{studentId} and teacher_id = #{teacherId}")
    Integer confirmStudentInRecord(Long id, Long studentId, Long teacherId);

    /**
     * 教师确认学生，第二步，更新数据库
     */
    @Update("update auth.subject set selected = 1, student_id = #{studentId} where id = #{id} and teacher_id = #{teacherId}")
    Integer confirmStudent(Long id, Long studentId, Long teacherId);

    /**
     * 教师确认学生，第三步，删除选题记录中所有与某学生相关的记录
     */
    @Delete("delete from auth.selection_record where student_id = #{studentId}")
    void deleteAllRecord(Long studentId);
}
